//
//  MQPictureInsterestingCell.m
//  MQ
//
//  Created by 曾杰 on 15/10/26.
//  Copyright © 2015年 徐永祥. All rights reserved.
//

#import "MQPictureInsterestingCell.h"
#import <UIImageView+WebCache.h>
#import "MQFMDBManager.h"
@implementation MQPictureInsterestingCell
-(NSMutableArray *)zanArr
{
    if (_zanArr == nil) {
        self.zanArr = [NSMutableArray arrayWithCapacity:0];
    }return _zanArr;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _backgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10, MQWidth-20, 200)];
        [_backgroundImageView setImage:[UIImage imageNamed:@"bg_share_large"]];
        [self.contentView addSubview:_backgroundImageView];
        
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,10,_backgroundImageView.bounds.size.width-20,30)];
        _titleLabel.numberOfLines = 0;
        _titleLabel.font = [UIFont systemFontOfSize:18];
        [_backgroundImageView addSubview:_titleLabel];
        
        _contentImgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 50, _backgroundImageView.bounds.size.width-20, 80)];
        [_backgroundImageView addSubview:_contentImgView];
        
        _likeImgView = [[UIImageView alloc]initWithFrame:CGRectMake(_backgroundImageView.bounds.size.width/2-20, 140, 20, 20)];
        [_likeImgView setImage:[UIImage imageNamed:@"star_unfav@2x"]];
        [_backgroundImageView addSubview:_likeImgView];
        
        _zanButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _zanButton.frame = CGRectMake(10, 140, 20, 20);
        [_zanButton setImage:[UIImage imageNamed:@"unzan" ] forState:(UIControlStateNormal)];
        [_backgroundImageView addSubview:_zanButton];
        
        _zanLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 140, 40, 20)];
        [_backgroundImageView addSubview:_zanLabel];
        
        _shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _shareButton.frame = CGRectMake(_backgroundImageView.bounds.size.width-40, 140, 20, 20);
        [_shareButton setImage:[UIImage imageNamed:@"fenxiang"] forState:(UIControlStateNormal)];
        [_backgroundImageView addSubview:_shareButton];
        
    }
    return self;
}
//cell的赋值
- (void)cellForModel:(MQPictureInsterestingModel *)model
{
    self.model = model;
    CGFloat strHeight = [MQPictureInsterestingCell cellForHeight:model.wbody];
     NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSArray *zanArr = [user objectForKey:@"zanArr"];
    if ([zanArr containsObject:model.wbody]) {
        model.iszan = YES;
    }else
    {
        model.iszan = NO;
    }
    if (model.iszan == YES) {
        NSInteger ZC = [model.likes integerValue];
        self.zanLabel.text = [NSString stringWithFormat:@"%ld",ZC+1];
        [_zanButton setImage:[UIImage imageNamed:@"zan"] forState:(UIControlStateNormal)];
        self.zanLabel.textColor = [UIColor redColor];
    }
    else
    {
        [_zanButton setImage:[UIImage imageNamed:@"unzan"] forState:(UIControlStateNormal)];
        self.zanLabel.textColor = [UIColor blackColor];
        NSInteger like = [model.likes integerValue];
        self.zanLabel.text = [NSString stringWithFormat:@"%ld",like];
    }
     self.zanLabel.frame = CGRectMake(30, strHeight+40+[model.wpic_m_height floatValue], 100, 20);
    self.zanButton.frame = CGRectMake(10, strHeight+40+[model.wpic_m_height floatValue], 20, 20);
     [self.zanButton addTarget:self action:@selector(changeZancount:) forControlEvents:(UIControlEventTouchUpInside)];
  NSArray *arr = [self selectPicDataBase];
    if ([arr containsObject:model.wbody]) {
        model.islike = YES;
    }else
    {
        model.islike = NO;
    }
    if (model.islike == YES) {
         i += 1;
        [self.likeImgView setImage:[UIImage imageNamed:@"star_faved@2x"]];
    }
    else
    {
        [self.likeImgView setImage:[UIImage imageNamed:@"star_unfav@2x"]];
    }
   
    UITapGestureRecognizer *likeTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeLike:)];
    [self.likeImgView addGestureRecognizer:likeTap];
    
    self.titleLabel.text = model.wbody;
    self.titleLabel.frame = CGRectMake(10, 10, MQWidth-20, strHeight);
    
    [self.contentImgView sd_setImageWithURL:[NSURL URLWithString:model.wpic_middle]];
    self.contentImgView.frame = CGRectMake(10, strHeight+20, MQWidth-40, [model.wpic_m_height floatValue]);
    
    self.likeImgView.frame = CGRectMake(_backgroundImageView.bounds.size.width/2-10, strHeight+40+[model.wpic_m_height floatValue], 20, 20);
    self.backgroundImageView.frame = CGRectMake(10, 10, MQWidth-20, strHeight+80+[model.wpic_m_height floatValue]);
    self.backgroundImageView.userInteractionEnabled = YES;
    self.likeImgView.userInteractionEnabled = YES;
    
   
    self.shareButton.frame = CGRectMake(_backgroundImageView.bounds.size.width-40, strHeight+40+[model.wpic_m_height floatValue], 20, 20);
}



// 点击收藏后变为红色
- (void)changeLike:(UIButton *)button
{
    i++;
    [self.likeImgView setImage:[UIImage imageNamed:(i%2==0?@"star_unfav@2x":@"star_faved@2x")]];
    CAKeyframeAnimation *k = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    k.values = @[@(0.1),@(1.0),@(1.5)];
    k.keyTimes = @[@(0.0),@(0.5),@(0.8),@(1.0)];
    k.calculationMode = kCAAnimationLinear;
    [self.likeImgView.layer addAnimation:k forKey:@"SHOW"];
    
    if (i%2 != 0) {
        self.model.islike = YES;
        [self animationLabel:@"收藏成功"];
        NSInteger isLike = self.model.islike ;
        NSString *islike = [NSString stringWithFormat:@"%ld",(long)isLike];
        NSInteger isZan = self.model.iszan;
        NSString *iszan = [NSString stringWithFormat:@"%ld",(long)isZan];
        [[MQFMDBManager shareDataBase] executeUpdate:@"INSERT INTO PICTURES (wbody, wpic_middle, wpic_m_height,islike,iszan) VALUES (?,?,?,?,?)",self.model.wbody,self.model.wpic_middle,self.model.wpic_m_height,islike,iszan];
    }
    else
    {
        self.model.islike = NO;
        [self animationLabel:@"取消收藏"];
        [[MQFMDBManager shareDataBase] executeUpdate:@"DELETE FROM PICTURES WHERE wbody = ?",self.model.wbody];
    }
    
}

//文字自适应
+ (CGFloat)cellForHeight:(NSString *)string
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18] forKey:NSFontAttributeName];
    CGRect bound = [string boundingRectWithSize:CGSizeMake(MQWidth-20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return bound.size.height;
}
// cell的行高
+ (CGFloat)HeightForModel:(MQPictureInsterestingModel *)model
{
    CGFloat strHeight = [MQPictureInsterestingCell cellForHeight:model.wbody];
    return strHeight+100+[model.wpic_m_height floatValue];
}
//点赞按钮+1
- (void)changeZancount:(UIButton *)button
{
    if (self.model.iszan == YES)
    {
        [self animationLabel:@"你已经点过赞了"];
    }
    else
    {
        NSString *zanCount = self.zanLabel.text;
        NSInteger ZC = [zanCount integerValue];
        self.zanLabel.text = [NSString stringWithFormat:@"%ld",ZC+1];
        [button setImage:[UIImage imageNamed:@"zan"] forState:(UIControlStateNormal)];
        self.zanLabel.textColor = [UIColor redColor];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, -20, 20, 20)];
        label.text = @"+1";
        [self.zanLabel addSubview:label];
        [UIView animateWithDuration:1.5 animations:^{
            label.frame = CGRectMake(10, 0, 20, 20);
            label.textColor = [UIColor redColor];
        } completion:^(BOOL finished) {
            label.frame =CGRectZero;
        }];
        self.model.iszan = YES;
        [self.zanArr addObject:self.model.wbody];
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:self.zanArr forKey:@"zanArr"];
    }
    
}

//查询数据库收藏
- (NSArray *)selectPicDataBase
{
    NSMutableArray *arr = [NSMutableArray array];
    FMResultSet *resultSet = [[MQFMDBManager shareDataBase] executeQuery:@"SELECT * FROM PICTURES"];
    while ([resultSet next]) {
        NSString *wbody = [resultSet stringForColumn:@"wbody"];
        [arr addObject:wbody];
    }
    [resultSet close];
    return arr;
}
//提示动画
- (void)animationLabel:(NSString *)message
{
    UILabel *messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _backgroundImageView.bounds.size.height-80, _backgroundImageView.bounds.size.width, 40)];
    [_backgroundImageView addSubview:messageLabel];
    messageLabel.text = message;
    messageLabel.textAlignment = NSTextAlignmentCenter;
    messageLabel.backgroundColor = [UIColor blackColor];
    messageLabel.alpha = 0.8;
    messageLabel.textColor = [UIColor whiteColor];
    [UIView animateWithDuration:1.5 animations:^{
        messageLabel.frame = CGRectMake(0, _backgroundImageView.bounds.size.height-80, _backgroundImageView.bounds.size.width, 40);
        messageLabel.alpha = 0.3;
    } completion:^(BOOL finished) {
        messageLabel.frame = CGRectZero;
        messageLabel.alpha = 0;
    }];

}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
